<template>
  <div>
    <el-dialog
      :title="ruleForm.id == '' ? '添加传感器' : '编辑传感器'"
      :visible.sync="addEditIsDialog_c"
      @close="resetForm('formName')"
      width="800px"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="formName"
        label-width="120px"
        class="demo-ruleForm"
        :class="{ 'form-rules-show': seeType }"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称：" prop="companyId">
              <el-cascader
                v-model="ruleForm.companyId"
                placeholder="请选择公司名称"
                :options="companyArray"
                :props="{
                  checkStrictly: true,
                  value: 'id',
                  label: 'name',
                  children: 'childList',
                }"
                @change="changeTreeItem"
                clearable
                :disabled="seeType"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基地名称：" prop="baseId">
              <el-select
                v-model="ruleForm.baseId"
                placeholder="请选择基地名称"
                @change="baseChange"
                clearable
              >
                <el-option
                  v-for="item in baseArray"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="控制箱名称：" prop="boxId">
              <el-select
              v-model="ruleForm.boxId"
              placeholder="请选择控制箱名称"
              @change="boxChange"
              clearable
            >
              <el-option
                v-for="item in boxArray"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="鱼池名称：" prop="fishpondId">
              <el-select
              v-model="ruleForm.fishpondId"
              placeholder="请选择鱼池名称"
              clearable
            >
              <el-option
                v-for="item in fishpondArray"
                :key="item.fishpondId"
                :label="item.fishpondName"
                :value="item.fishpondId"
              ></el-option>
            </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编码：" prop="deviceCode">
              <el-input
                v-model="ruleForm.deviceCode"
                placeholder="请输入设备编码"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名称：" prop="deviceName">
              <el-input
                v-model="ruleForm.deviceName"
                placeholder="请输入设备名称"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
              <el-form-item label="设备类型：" prop="deviceType"
              ><el-select
                v-model="ruleForm.deviceType"
                placeholder="请选择设备类型"
                clearable
              >
                <el-option label="电流" :value="0"></el-option>
                <el-option label="电压" :value="1"></el-option>
                <el-option label="水位" :value="2"></el-option>
                <el-option label="气压" :value="3"></el-option>
                <el-option label="流量" :value="4"></el-option>
                <el-option label="溶氧" :value="5"></el-option>
                <el-option label="相序状态" :value="6"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="属性编码：" prop="attributeCode">
              <el-input
                v-model="ruleForm.attributeCode"
                placeholder="请输入属性编码"
                clearable
                maxlength="50"
                show-word-limit
                :disabled="seeType"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button class="btn-light-blue" @click="submitForm('formName')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import {
  getConfigFishpondDeviceNumericalRelation,saveConfigFishpondDeviceNumericalRelation,getBoxFishponds
} from "@/api/device";
import { getCompanyInfoTree } from "@/api/company";
import { getSysCompanyBases,getObjBoxs } from "@/api/system";

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: "",
    },
    addEditStatus: {
      type: String,
      default: "add",
    },
    addEditIsDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: "",
        companyId: "",
        baseId: "",
        boxId: "",
        fishpondId: "",
        deviceCode: "",
        deviceName: "",
        deviceType: "",
        attributeCode: "",
      },
      rules: {
        // 表单规则
        companyId: [
          { required: true, message: "请选择公司名称", trigger: "change" },
        ],
        baseId: [
          { required: true, message: "请选择基地名称", trigger: "change" },
        ],
        boxId: [{ required: true, message: "请选择控制箱名称", trigger: "change" }],
        fishpondId: [{ required: true, message: "请选择鱼池名称", trigger: "change" }],
        deviceCode: [{ required: true, message: "请输入设备编码", trigger: "blur" }],
        deviceName: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
        deviceType: [{ required: true, message: "请选择设备类型", trigger: "change" }],
        attributeCode: [{ required: true, message: "请输入属性编码", trigger: "blur" }],
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      baseArray: [], // 基地集合
      companyArray: [], // 公司集合
      boxArray: [], // 控制箱集合
      fishpondArray: [], // 鱼池集合
      userType: "", // 用户类型（0超级管理员，1公司管理员）
    };
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val;
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == "add") {
          this.ruleForm.id = "";
          this.seeType = false;
        }

        // 编辑
        if (this.addEditStatus == "edit") {
          this.ruleForm.id = this.addEditId;
          this.seeType = false;
          this.getDetail();
        }

        // 查看
        if (this.addEditStatus == "see") {
          this.ruleForm.id = this.addEditId;
          this.seeType = true;
          this.getDetail();
        }

        this.userType = Number(window.localStorage.getItem("userType"));

         // 获取公司列表
         this.getCompanyInfoTree();

        this.getSysCompanyBases();
      }
    },
  },
  methods: {
    // 详情
    getDetail() {
      getConfigFishpondDeviceNumericalRelation({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.data;

          // 获取公司列表
          this.getCompanyInfoTree();

          this.getSysCompanyBases();

          this.getObjBoxs();

          this.getBoxFishponds();
        }
      });
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveConfigFishpondDeviceNumericalRelation(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });

              // 关闭弹窗
              this.$parent.closeAddEdit();
              // 刷新列表
              this.$parent.onSubmit();
            }
          });
        }
      });
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$parent.closeAddEdit();
    },
    // 获取公司列表
    getCompanyInfoTree() {
      if (this.ruleForm.userType === "") {
        return;
      }

      if (this.ruleForm.userType == 0) {
        // 超级管理员，公司只有一个总公司
        this.companyArray = [
          {
            id: 0,
            name: "总公司",
            parentId: 0,
          },
        ];
      } else {
        getCompanyInfoTree({}).then((res) => {
          if (res.code == 200) {
            this.companyArray = res.data;
          }
        });
      }
    },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.ruleForm.companyId = val[val.length - 1];
      } else {
        this.ruleForm.companyId = "";
      }

      this.ruleForm.baseId = "";
      this.baseArray = [];

      // 获取基地列表
      this.getSysCompanyBases();
    },
    // 获取基地列表
    getSysCompanyBases() {
      if (this.ruleForm.companyId === "" || this.ruleForm.companyId == null) {
        return;
      }

      getSysCompanyBases({ id: this.ruleForm.companyId }).then((res) => {
        if (res.code == 200) {
          this.baseArray = res.data;
        }
      });
    },
    // 基地下拉切换
    baseChange(){
      this.ruleForm.boxId = "";
      this.boxArray = [];

      // 获取控制箱列表
      this.getObjBoxs();
    },
    // 获取控制箱列表
    getObjBoxs(){
      if (
        this.ruleForm.baseId === "" ||
        this.ruleForm.baseId == null
      ) {
        return;
      }
      
      getObjBoxs({ id: this.ruleForm.baseId }).then((res) => {
        if (res.code == 200) {
          this.boxArray = res.data;
        }
      });
    },
     // 控制箱下拉切换
     boxChange(){
      this.ruleForm.fishpondId = "";
      this.fishpondArray = [];

      // 获取鱼池列表
      this.getBoxFishponds();
    },
    // 获取鱼池列表
    getBoxFishponds(){
      if (
        this.ruleForm.boxId === "" ||
        this.ruleForm.boxId == null
      ) {
        return;
      }
      
      getBoxFishponds({ id: this.ruleForm.boxId }).then((res) => {
        if (res.code == 200) {
          this.fishpondArray = res.data;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;
}

.el-cascader {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
